# -*- coding:utf-8 -*-

import logging
import os
from logging import handlers

_LOG_PATH = os.path.dirname(os.path.abspath(__file__)) + r'/../logs/'

_LOG_FORMAT = "%(asctime)s %(name)s %(levelname)s %(message)s"
_DATE_FORMAT = r'%Y-%m-%d %H:%M:%S %A'


class MyLog:

    def __init__(self):
        self.__create_log_path()
        self.logger = self.get_logger()

    @staticmethod
    def get_logger():
        """
        获取logger
        :return:
        """
        logger = logging.getLogger(__name__)
        if not logger.handlers:
            format_ = logging.Formatter(fmt=_LOG_FORMAT, datefmt=_DATE_FORMAT)
            file_handler = handlers.TimedRotatingFileHandler(filename=_LOG_PATH + 'log.log')
            file_handler.setFormatter(format_)
            logger.addHandler(file_handler)
            logger.setLevel(logging.INFO)
        return logger

    @staticmethod
    def __create_log_path():
        """
        创建日志目录
        :return:
        """
        if not os.path.exists(_LOG_PATH):
            os.mkdir(_LOG_PATH)
